﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Monocle.Controllers.StaticHelpers;
using Monocle.DataProvider;
using Monocle.Models.Admin;

namespace Monocle.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class AdminController : Controller
    {

        /// <summary>
        /// Retrieve the stylesheet for the App and show an editing view
        /// </summary>
        /// <returns></returns>
        public ActionResult FileEdit()
        {
            var vm = new FileViewModel();
            vm.Files = BuildFileStructure();
            vm.File = vm.Files.FirstOrDefault().Value;
            vm.FileExt = Path.GetExtension(vm.File);
            var content = string.Empty;
            try
            {
                using (var stream = new StreamReader(vm.Files.FirstOrDefault().Value))
                {
                    content = stream.ReadToEnd();
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
            vm.Contents = content;
            return View(vm);
        }

        [HttpPost]
        public ActionResult FileEdit(String filePath)
        {
            var vm = new FileViewModel();
            vm.Files = BuildFileStructure();
            vm.File = filePath;
            vm.FileExt = Path.GetExtension(vm.File);
            var content = string.Empty;
            try
            {
                using (var stream = new StreamReader(filePath))
                {
                    content = stream.ReadToEnd();
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
            vm.Contents = content;
            return View("FileEdit", vm);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SaveFile(FileViewModel vm)
        {
            try
            {
                System.IO.File.WriteAllText(vm.File, vm.Contents);
            }
            catch (Exception exc)
            {
                throw exc;
            }
            ViewBag.Status = Resources.Monocle.Generic_Saved;
            return FileEdit(vm.File);
        }

        /// <summary>
        /// Build a dictionary of editable files found in a set, set of folders
        /// </summary>
        /// <returns></returns>
        private Dictionary<String, String> BuildFileStructure()
        {
            var dictionary = new Dictionary<string, string>();
            var folderList = new List<String>();
            folderList.Add(ConfigurationManager.AppSettings["App_Path"]);
            folderList.Add(ConfigurationManager.AppSettings["App_Path"] + "/js/");
            folderList.Add(ConfigurationManager.AppSettings["App_Path"] + "/styles/");

            foreach (var s in folderList)
            {
                var fileList = Directory.GetFiles(s);
                foreach (var file in fileList)
                {
                    var name = Path.GetFileName(file);

                    var ext = Path.GetExtension(file);
                    if (ext == ".html" || ext == ".htm" || ext == ".css" || ext == ".js")
                        dictionary.Add(name, file);
                }
            }
            return dictionary;
        }

    }
}
